layui.use(['jquery','layer','table','form'],function(){
    var $=layui.jquery;
    var layer=layui.layer;
    var table=layui.table;
    var form = layui.form;

    //获取user
    var user = localStorage.getItem("user");
    user = JSON.parse(user);
    var id = user.data.userId;

    //表格
    getTable();
    function getTable(){
        $.ajax({
            url:"/product/getPersonalProducts",
            type:"post",
            dataType:"json",
            data:{"createId" : id},
            success:function (data) {
                demoTable(data.data);
            }
        })
    }

    //表格
    function demoTable(data){
        var tableRender = table.render({
            elem:'#demoTable',
            title:"委托书",
            data:data,
            toolbar:'#button_ids',
            limit:5,
            width:1650,
            page:{
                layout:['count','prev','page','next','limit','skip','refresh'],
                limits:[5,10,20],
                first:"首页",
                last:"尾页",
            },
            cols:[[
                {type:'checkbox',width:50},
                {field:"productCode",title:"商品编号",align:'center',width:177},
                {field:"mainImage",title:"商品图片",align:'center',width:178,templet:function(data){
                        return '<img src="'+data.mainImage+'"';
                    }},
                {field:"productName",title:"商品名称",align:'center',width:177},
                {field:"searchKeywords",title:"关键字",align:'center',width:178},
                {field:"currentpriceProduct",title:"现价",align:'center',width:177},
                {field:"result",title:"审核状态",align:'center',width:178,templet:function(data){
                        if(data.result==1){
                            return '审核通过';
                        }else if(data.result==2){
                            return '待审核';
                        }else{
                            return '审核驳回';
                        }
                        return '<p>'+data.result+'</p><p style="color: #00B83F">审核详情</p>';
                    }},
                {field:"productSate",title:"上架/下架",align:'center',width:177,templet:function(data){
                        if(data.productSate==1){
                            return '<input type=\"checkbox\" value="'+data.productCode+'" lay-skin=\"switch\" lay-text=\"上架|下架"\ checked lay-filter="switchTest">';
                        }else if(data.productSate==2){
                            return '<input type=\"checkbox\" value="'+data.productCode+'" lay-skin=\"switch\" lay-text=\"上架|下架" lay-filter="switchTest"\>';
                        }
                    }},
                {field:"hits",title:"销量",align:'center',width:178},
                {field:"title",title:"商品分类",align:'center',width:125},
                {field:"productLabel",title:"商品标签",align:'center',width:125},
                {field:'',title:"操作",align:'center',width:177,toolbar:'#button_caozuo'}
            ]],
            done: function () {
                $("[data-field='title']").css('display','none');
                $("[data-field='productLabel']").css('display','none');

            }
        })

    }

    form.on('switch(switchTest)', function(data){
        if(data.elem.checked==true){
            $.ajax({
                url:"/product/updateProductState",
                type:"post",
                dataType:"json",
                data:{'productSate':1,'productCode':data.value},
                success:function (data) {
                    layer.msg("修改成功");
                }
            })
        }else{
            $.ajax({
                url:"/product/updateProductState",
                type:"post",
                dataType:"json",
                data:{'productSate':2,'productCode':data.value},
                success:function (data) {
                    layer.msg("修改成功");
                }
            })
        }
    })

    //搜索个人商品的模糊查
    getTableSearch = function getTableSearch(productName,searchKeywords,currentpriceProduct){
        $.ajax({
            url:"/product/getPersonalProductsSearch",
            type:"post",
            dataType:"json",
            data:{'productName':productName,'searchKeywords': searchKeywords,'currentpriceProduct':currentpriceProduct},
            success:function (data) {
                demoTable(data.data);
            }
        })
    };

    //搜索
    search = function search(){
        var productName = $("input[name=productName]").val();
        var searchKeywords = $("input[name=searchKeywords]").val();
        var currentpriceProduct = $("input[name=currentpriceProduct]").val();
        getTableSearch(productName,searchKeywords,currentpriceProduct);
    }

    //高级检索
    retrieval=function retrieval(){
        layer.open({
            type : 2,
            title : "样品分类",
            area : [ '900px', '550px' ],
            shade : 0,
            offset: 'auto',
            content :"product-sample.html",
            end:function(){
                var data=$("input[name=ProductRetrieval]").data("Productdata");
                if(data!=null){
                    demoTable(data);
                }
            }
        })
    }

    //右边工具栏操作
    table.on('tool(demoTable)',function (obj) {
        var event = obj.event;
        if (event == "selectTable"){
            layer.open({
                type:2,
                title:"查看",
                area:["1000px","700px"],
                content:"/product/productManage/productList/product-list-tool-select.html",
                success:function(layero,index){
                    console.log(obj.data);
                    var child = layero.find('iframe').contents();
                    child.find("#productCode").text(obj.data.productCode);
                    child.find("#productName").text(obj.data.productName);
                    $.ajax({
                        url:"/product/getUserCollection",
                        data:{"productCode":obj.data.productCode},
                        type:'post',
                        dataType:'json',
                        success:function (data) {
                            console.log(data.data);
                            child.find("#Collection").text(data.data);
                        }
                    })
                    $.ajax({
                        url:"/product/getUserOrder",
                        data:{"productCode":obj.data.productCode},
                        type:'post',
                        dataType:'json',
                        success:function (data) {
                            console.log(data.data);
                            child.find("#salesVolume").text(data.data);
                        }
                    })

                    child.find("#currentpriceProduct").text(obj.data.currentpriceProduct);
                    child.find("#title").text(obj.data.title);
                    if(obj.data.productSate == 1){
                        child.find("#productSate").text("上架");
                    }else if(obj.data.productSate == 2){
                        child.find("#productSate").text("下架");
                    }
                    child.find("#productDescription").text(obj.data.productDescription);
                    child.find("#hits").text(obj.data.hits);
                    child.find("#mainImage").attr("src",obj.data.mainImage);
                    child.find("#productLabel").text(obj.data.productLabel);
                },
                end:function (layero, index) {
                    layer.close(index);
                }
            });
        }else if(event == "deleteTable"){
             var productCode = obj.data.productCode;
             var productData = obj.data;
             $.ajax({
                 url:"/product/deleteByProductCodeProduct",
                 data:{'productCode':productCode},
                 type:'post',
                 dataType:'json',
                 success:function (data) {
                     getTable();
                 }
             })
        }

    });

    //刷新
    refresh =function refresh(){
        location.reload();
    };

    //返回

});